package Controller.Sign;

import Model.SignModel.DeviceHg;
import Model.User;
import Service.DeviceHgService;
import Service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import utils.permission.ConstantsUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping(value = "/DeviceHg")
public class DeviceHgController {

    @Autowired
    private DeviceHgService deviceHgService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/getDeviceHgList")
    @ResponseBody
    public PageInfo getDeviceList(@RequestParam(value = "pn", defaultValue = "1") Integer pn, @RequestParam(value = "limit", defaultValue = "10") Integer limit, HttpServletRequest request, DeviceHg deviceHg){
        User user = (User) request.getSession().getAttribute(ConstantsUtils.SessionObj.CURRENT_USER);
        String Role_id=this.userService.findRoleIDbyUserNumber(user.getUser_number());
        if(Role_id.equals("2")){
            String settleCode =userService.findSettleCodebyUserNumber(user.getUser_number());
            System.out.println("找到机构代码："+settleCode);
            deviceHg.setOrganization_id(settleCode);
            PageHelper.startPage(pn, limit);
            List<DeviceHg> deviceHgList = deviceHgService.getDeviceHgList(deviceHg);
            System.out.println("[获取到护工设备数据：]"+deviceHgList);
            PageInfo page = new PageInfo(deviceHgList);
            return page;
        }else{
            PageHelper.startPage(pn, limit);
            List<DeviceHg> deviceHgList = deviceHgService.getDeviceHgList(deviceHg);
            System.out.println("[获取到设备数据：]"+deviceHgList);
            PageInfo page = new PageInfo(deviceHgList);
            return page;
        }


    }

    @RequestMapping(value = "/updateHgDeviceState")
    @ResponseBody
    public int updateHgDeviceState(DeviceHg deviceHg){
        int status = 0;
        try{
            deviceHgService.updateHgDeviceState(deviceHg);
            System.out.println("[更新护工设备状态完成~]");
        }catch (Exception e){
            status = -1;
            e.printStackTrace();
        }
        return status;
    }

    @RequestMapping(value = "/deleteHgDevice")
    @ResponseBody
    public int deleteHgDevice(int id){
        int status = 0;
        try {
            deviceHgService.deleteHgDevice(id);
            System.out.println("删除护工设备成功~");
        }catch (Exception e){
            status = -1;
            e.printStackTrace();
        }
        return status;
    }

    @RequestMapping(value = "/updateHgDevice")
    @ResponseBody
    public int updateHgDevice(DeviceHg deviceHg){
        int status = 0;
        try {
            deviceHgService.updateHgDevice(deviceHg);
            System.out.println("更新护工设备完成~");
        }catch (Exception e){
            status = -1;
            e.printStackTrace();
        }
        return status;
    }

    @RequestMapping(value = "/addHgDevice")
    @ResponseBody
    public int addHgDevice(DeviceHg deviceHg,HttpServletRequest request){
        int status = 0;
        User user = (User) request.getSession().getAttribute(ConstantsUtils.SessionObj.CURRENT_USER);
        String settleCode =userService.findSettleCodebyUserNumber(user.getUser_number());
        System.out.println("当前机构ID为："+settleCode);
        deviceHg.setOrganization_id(settleCode);
        String organization = user.getUser_name();
        System.out.println("当前机构为："+organization);
        deviceHg.setOrganization(organization);
        try {
            deviceHgService.addHgDevice(deviceHg);
            System.out.println("添加护工设备成功~");
        }catch (Exception e){
            status = -1;
            e.printStackTrace();
        }
        return status;
    }

    @RequestMapping(value = "/getHgDevice")
    @ResponseBody
    public List<DeviceHg> getHgDevice(HttpServletRequest request, DeviceHg deviceHg){
        deviceHg.setState(-1);
        User user = (User) request.getSession().getAttribute(ConstantsUtils.SessionObj.CURRENT_USER);
        String Role_id=this.userService.findRoleIDbyUserNumber(user.getUser_number());
        if (Role_id.equals("2")){
            String settleCode =userService.findSettleCodebyUserNumber(user.getUser_number());
            System.out.println("找到机构代码："+settleCode);
            deviceHg.setOrganization_id(settleCode);
            List<DeviceHg> list = deviceHgService.getHgDevice(deviceHg);
            System.out.println("获取到护工设备一列："+list);
            return list;
        }else{
            List<DeviceHg> list = deviceHgService.getHgDevice(deviceHg);
            System.out.println("获取到护工设备一列："+list);
            return list;
        }
    }
}
